library(mice) # Boys dataset library(tidyverse)# All the good stuff library(ggplot2) # Plotting suite (actually included in tidyverse)
library(mice) # Boys dataset library(tidyverse)# All the good stuff library(ggplot2) # Plotting suite (actually included in tidyverse)
Source: https://www.autodeskresearch.com/publications/samestats
ggplot2?Layered plotting based on the book The Grammar of Graphics by Leland Wilkinsons.
With ggplot2 you
ggplot2 then takes care of the details
The package is extremely popular and well described.
1: Provide the data
boys |> ggplot()
2: state which geometric object to display
boys |> ggplot() |> geom_point()
3: map variable to aesthetics
boys |> ggplot() + geom_point(aes(x = hgt, y = wgt)
Create the plot
gg <- boys |>
ggplot() +
geom_point(aes(x=hgt, y=wgt),
col="dark green" )
Add another layer (smooth fit line)
gg <- gg +
geom_smooth(aes(x = hgt, y = wgt),
col = "dark blue")
Give it some labels and a nice look
gg <- gg + labs(x = "Age", y = "BMI", title = "BMI trend for boys") + theme_minimal()
plot(gg)
gg <- boys |>
filter(!is.na(reg)) |>
ggplot() +
geom_point(aes(x = hgt,
y = wgt,
shape = reg,
colour = age),
alpha = 0.5) +
labs(title = "Trend for boys",
x = "Height",
y = "Weight",
shape = "Region",
colour = "Age") +
theme_minimal()
plot(gg)
geom_point
geom_bar
geom_line
geom_smooth
geom_histogram
geom_boxplot
geom_density
Easy with ggsave()
# save as pdf
ggssave("plot.pdf", myplot)
# save as png and specify dimensions
ggssave("plot.png", myplot, width = 7, height = 5, units="in")
What is it
* R Markdown is a file format for making dynamic documents
Who is it for
* For communicating to decision makers, who want to focus on the conclusions, not the code behind the analysis.
For collaborating with others (including future you!), who are interested in both your conclusions, and how you reached them (i.e. the code).
As an environment in which to do data science, as a modern day lab notebook where you can capture not only what you did, but also what you were thinking.
To create a new document:
- File - New File - RMarkdown
You can create a title and an output-format and then RStudio sets up the basics for you.
It contains three important types of content:
…but just push (ctrl+shift+K) the Knit icon.
YAML - “yet another markup language”
RStudio creates a YAML header when starting from scratch with File \(\rightarrow\) New File \(\rightarrow\) R Markdown…
Examples
*italic* gives italic
**bold** gives bold
~~Strikethrough~~ gives Strikethrough
superscript^2^subscript~2~ gives superscript2/subscript2
$e^{i\pi}+1=0$ gives \(e^{i\pi}+1=0\)
`r nrow(mice::boys)` gives 748
Headers:
# Level 1 header
## Level 2 header
* Blah Blah* Blah
gives
1. Blah Blah2. Blah
gives
To integrate R code into your document you create a code chunk (Short cut Ctrl+Alt+i).
By default it will show both your code and the result from the console. So the following codechunk in your Rmarkdown file:
```{r}
a <- 100
a*2
```
Will be printed in your document as:
a <- 100 a*2
## [1] 200
When writing your Rmarkdown-file you can run each line in your chunk with Ctrl+Enter or the entire chunk with Ctrl+Shift+Enter
Printing tables to markdown aren’t pretty. But with the package kableExtra it is easy to make readable tabels.
boys |> select(-gen, -phb, -tv) |> head()
## age hgt wgt bmi hc reg ## 3 0.035 50.1 3.650 14.54 33.7 south ## 4 0.038 53.5 3.370 11.77 35.0 south ## 18 0.057 50.0 3.140 12.56 35.2 south ## 23 0.060 54.5 4.270 14.37 36.7 south ## 28 0.062 57.5 5.030 15.21 37.3 south ## 36 0.068 55.5 4.655 15.11 37.0 south
knitr::kableboys |>
select(-gen, -phb, -tv, -hc) |>
head() |>
knitr::kable(format = "html",
col.names = c("Age","Height","Weight","BMI", "Region"),
align = "ccccc",
caption = "The 5 first boys")
| Age | Height | Weight | BMI | Region | |
|---|---|---|---|---|---|
| 3 | 0.035 | 50.1 | 3.650 | 14.54 | south |
| 4 | 0.038 | 53.5 | 3.370 | 11.77 | south |
| 18 | 0.057 | 50.0 | 3.140 | 12.56 | south |
| 23 | 0.060 | 54.5 | 4.270 | 14.37 | south |
| 28 | 0.062 | 57.5 | 5.030 | 15.21 | south |
| 36 | 0.068 | 55.5 | 4.655 | 15.11 | south |
knitr::kable and KableExtraboys |>
select(-gen, -phb, -tv, -hc) |>
head() |>
knitr::kable(col.names = c("Age","Height","Weight","BMI", "Region"),
align = "ccccc",
caption = "The 5 first boys") |>
kable_styling(bootstrap_options = c("hover"),
full_width = FALSE, position = "left")
| Age | Height | Weight | BMI | Region | |
|---|---|---|---|---|---|
| 3 | 0.035 | 50.1 | 3.650 | 14.54 | south |
| 4 | 0.038 | 53.5 | 3.370 | 11.77 | south |
| 18 | 0.057 | 50.0 | 3.140 | 12.56 | south |
| 23 | 0.060 | 54.5 | 4.270 | 14.37 | south |
| 28 | 0.062 | 57.5 | 5.030 | 15.21 | south |
| 36 | 0.068 | 55.5 | 4.655 | 15.11 | south |
Rmarkdown will notice when a code chunk produces a figure.
boys |>
ggplot() +
geom_point(aes(x = age,
y = hgt))
## Warning: Removed 20 rows containing missing values (`geom_point()`).
Sometimes you do not want to show your code, but only your results. Then you can use a chunk option.
The option that hides the code in the output is called echo
```{r echo = F}
a <- 100
a*2
```
## [1] 200
x marks the thing that the chunk will not do
RMarkdown
ggplot2
ggplot written by Hadley Wickham who is also the author to the package. It is quite long, but if you want to truely understand it, this is the place to start.ggplot2 so it can make mapstidyverse.